<template>
	<div>
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="发起机构" prop="ownerOrgId" label-width="70px">
        <el-input
          v-model="queryParams.ownerOrgId"
          placeholder="请输入发起机构"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="status" label-width="70px">
        <el-select v-model="queryParams.status" placeholder="请选择任务状态" clearable size="small">
          <el-option
            v-for="dict in applyStatusOptOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>

      <CreateDialog ref="createDialog" @refresh="getList"></CreateDialog>
      <ViewDialog ref="viewDialog" @refresh="getList"></ViewDialog>
      <RectifyTaskDialog ref="rectifyTaskDialog" @refresh="getList"></RectifyTaskDialog>
    </el-form>

    <el-table v-loading="loading" :data="labSafetyFormList">
      <el-table-column label="序号" align="center" prop="instanceId" />
      <el-table-column label="名称" align="center" prop="flowName" :show-overflow-tooltip="true"/>
      <el-table-column label="任务类型" align="center" prop="type" :formatter="typeFormat" />
      <el-table-column label="安检类型" align="center" prop="safetyCheckOpt" :formatter="safetyCheckOptFormat" />
      <el-table-column label="任务周期" align="center" prop="taskCycleOpt" :formatter="taskCycleOptFormat" />
      <el-table-column label="开始时间" align="center" prop="startDate" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="endDate" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="流程状态" align="center" prop="applyStatusOpt" :formatter="flowStatusOptFormat" />
      <el-table-column label="操作" align="center" fixed="right" width="180">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleView(scope.row)"
          >查看</el-button>
          <el-button
            v-if= "scope.row.flowStatusOpt=='uncommitted' && checkTaskUser(scope.row)"
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
          >编辑</el-button>
          <el-button
            v-if= "!scope.row.checkUserId"
            size="mini"
            type="text"
            @click="handleSign(scope.row)"
          >签收</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
	</div>
</template>
<script>
import { listTask, signTask } from "@/api/labsafety/task"
import CreateDialog from "./create/createDialog.vue"
import ViewDialog from "./view/viewDialog.vue"
import RectifyTaskDialog from "./create/rectifyTaskDialog.vue"
export default {
  name: "ToDoTask",
  components: { CreateDialog, ViewDialog, RectifyTaskDialog },
  data() {
    return {
      activeName: 'first',
      // 遮罩层
      loading: true,
      // 查询结果
      labSafetyFormList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        ownerOrgId: null,
        status: null
      },
      total: 0,
      applyStatusOptOptions: [],
      flowStatusOptOptions: [],
      safetyCheckOptOptions: [],
      safetyCheckTaskTypeOptOptions: [],
      taskCycleOptOptions: [],
      rectificationTypeOptOptions: [],
      safetyTemplateOptOptions: [],
      checkFrequencyOptOptions: [],
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("applyStatusOpt").then(response => {
      this.applyStatusOptOptions = response.data;
    });
    this.getDicts("flowStatusOpt ").then(response => {
      this.flowStatusOptOptions = response.data;
    });
    this.getDicts("safetyCheckOpt").then(response => {
      this.safetyCheckOptOptions = response.data;
    });
    this.getDicts("taskCycleOpt").then(response => {
      this.taskCycleOptOptions = response.data;
    });
    this.getDicts("safetyCheckTaskTypeOpt").then(response => {
      this.safetyCheckTaskTypeOptOptions = response.data;
    });
    // this.getDicts("rectificationTypeOpt").then(response => {
    //   this.rectificationTypeOptOptions = response.data;
    // });
    // this.getDicts("safetyTemplateOpt").then(response => {
    //   this.safetyTemplateOptOptions = response.data;
    // });
    // this.getDicts("checkFrequencyOpt").then(response => {
    //   this.checkFrequencyOptOptions = response.data;
    // });
	},
  methods: {
    /** 查询安全检查单列表 */
    getList() {
      this.loading = true;
      listTask(this.queryParams).then(response => {
        this.labSafetyFormList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
   // 领用状态字典翻译
    applyStatusOptFormat(row, column) {
      return this.selectDictLabel(this.applyStatusOptOptions, row.status);
    },
    // 流程状态字典翻译
    flowStatusOptFormat(row, column) {
      return this.selectDictLabel(this.flowStatusOptOptions, row.flowStatusOpt);
    },
    // 安检类型字典翻译
    typeFormat(row, column) {
      return this.selectDictLabel(this.safetyCheckTaskTypeOptOptions, row.type);
    },
    // 安检类型字典翻译
    safetyCheckOptFormat(row, column) {
      return this.selectDictLabel(this.safetyCheckOptOptions, row.safetyCheckOpt);
    },
    // 任务周期字典翻译
    taskCycleOptFormat(row, column) {
      return this.selectDictLabel(this.taskCycleOptOptions, row.taskCycleOpt);
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    handleUpdate(row) {
      console.log(row.type);
      if (row.type == 1) {
        this.$refs.createDialog.handleUpdate(row.instanceId)
      } else {
        this.$refs.rectifyTaskDialog.handleUpdate(row.instanceId)
      }
    },
    handleDelete(row) {
      delLabsafetyForm(row.instanceId).then(response => {
        this.getList()
      })
    },
    handleView(row) {
      this.$refs.viewDialog.handleView(row.instanceId)
    },
    handleSign(row) {
      signTask(row.instanceId).then(response => {
        this.msgSuccess("签收成功");
        this.getList()
      });
    },
    checkTaskUser(row) {
      const user = this.$store.state.user;
      // console.log(user.userId, row.checkUserId)
      return user.userId == row.checkUserId
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    }
  }
}
</script>
<style type="text/css" scoped>
  .form_long{
    width:475px;
  }
  .form_short{
    width:200px;
  }
  .el-button.active{
    text-decoration: underline;
    color: #0000ee;
  }
  .el-button.dis_active {
    color: #999999
  }
  .select_view {
    display: flex;
    flex-direction: row;
    align-items: center;
  }
</style>
